<?php defined('SYSPATH') or die('No direct script access.');

class Model_Admin extends Model_Database
{
    public function get_data($table)
    {
        return DB::select()
			->from($table)
            ->execute()
            ->as_array();
    }

    public function get_role($id = NULL)
    {
    	if (!$id)
    	{
    		return array(
    			'name'			=>'',
    			'description'	=>'',
    		);
    	}
    	return DB::select()
    	->from('roles')
    	->where('id','=',$id)
    	->cached(600)
    	->execute()
    	->current();
    }

    public function get_user($id = NULL)
    {
    	if (!$id)
    	{
    		return array(
    			'username'	=>'',
    			'email'		=>'',
    			'role_id'	=>'',
    		);
    	}
    	return $this->get_users($id);
    }
    
    public function get_roles()
    {
    	$arr = DB::select('id','name')
	    	->from('roles')
	    	->where('id','!=',1)
	    	->cached(600)
	    	->execute()
	    	->as_array();
    	$roles[] = 'new';
    	foreach($arr as &$role)
    	{
    		$roles[$role['id']] = $role['name'];
    	}
    	return $roles;
    }
    
    public function get_lead($leadid)
    {
    	return DB::select()
			->from('leads')
			->join('clients','LEFT')
			->on('leads.leadsid', '=', 'clients.lead_id')
			->where('leads.leadsid','=',$leadid)
			->limit(1)
			->execute()
			->current();
    }

    public function get_users($id = NULL)
    {
    	if (!empty($id) AND $id>1)
    	{
    		return DB::select(
    				array('users.id', 'id'), 
    				array('users.username', 'username'),
    				array('users.email', 'email'),
    				array('users.logins', 'logins'),
    				array('users.last_login', 'last_login'),
    				array('roles_users.role_id', 'role_id'),
    				array('roles.name', 'role_name')
    			)
    			->from('users')
    			->join('roles_users','LEFT')
    			->on('roles_users.user_id', '=', 'users.id')
    			->join('roles','LEFT')
    			->on('roles_users.role_id', '=', 'roles.id')
    			->where('users.id','>',1)
    			->and_where('users.id','=',$id)
    			->and_where('roles_users.role_id', '>', 1)
    			->limit(1)
    			->execute()
    			->current();
    	}
    	else
    	{
    		return DB::select(
    				array('users.id', 'id'),
    				array('users.username', 'username'),
    				array('users.email', 'email'),
    				array('users.logins', 'logins'),
    				array('users.last_login', 'last_login'),
    				array('roles_users.role_id', 'role_id'),
    				array('roles.name', 'role_name')
    			)
	    		->from('users')
	    		->join('roles_users','LEFT')
	    		->on('roles_users.user_id', '=', 'users.id')
	    		->join('roles','LEFT')
	    		->on('roles_users.role_id', '=', 'roles.id')
	    		->where('users.id','>',1)
	    		->where('roles_users.role_id', '>', 1)
	    		->execute()
	    		->as_array();
    	}    	
    }
    
    public function update_data($data)
    {
    	switch ($data['action']) {
    		case 'status':
    			//$this->truncate($data['action']);
    			//$insert = DB::insert($data['action'], array($data['action'].'text'));
    			foreach($data[$data['action']] as $id=>&$$data['action'])
    			{
    				$$data['action'] = UTF8::trim($$data['action']);
    				if (empty($$data['action']))
    				{
    					//unlink($$data['action'],$data['color'][$id]);
    				}
    			}

    			$insert = DB::insert($data['action'], array('statustest', 'color'));
    			//->values(array($lead['leadsid'], 1, $this->request->post('visit')))
    			//->execute();
    		break;
    		case 'additional':
    		case 'subjects':
    			$exec = FALSE;	
    			$this->truncate($data['action']);
    			$insert = DB::insert($data['action'], array($data['action'].'text'));  			
    			foreach($data[$data['action']] as $id=>&$$data['action'])
    			{
    				$$data['action'] = UTF8::trim($$data['action']);
    				if (!empty($$data['action']))
    				{
    					$insert->values(array($$data['action']));
    					$exec = TRUE;
    				}
    			}

    			if ($exec)
    			{
    				$insert->execute();
    			}
    			
    		break;
    	}
    }
    private function truncate($table)
    {
    	DB::query(Database::DELETE, "TRUNCATE TABLE `{$table}`")->execute();
    }
}